﻿using MYQQX.SP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MYQQX
{
    public partial class XYSetForm : Form
    {
        // 所有的稀有牌列表
        private List<ISp> spList = new List<ISp>();

        private GameInfo _GameInfo;
        internal MainForm mainForm;
        public XYSetForm(GameInfo gameInfo)
        {
            _GameInfo = gameInfo;
            InitializeComponent();
        }

        private void XYSetForm_Load(object sender, EventArgs e)
        {
            InitSource();
        }
        private void InitSource()
        {
            // 加载所有的稀有牌
            var asbs= AppDomain.CurrentDomain.GetAssemblies();
            foreach (var asb in asbs)
            {
                var tps = asb.GetTypes();
                foreach (var tp in tps)
                {
                    if (typeof(ISp).IsAssignableFrom(tp) && !tp.IsAbstract)
                    {
                        var model = System.Activator.CreateInstance(tp) as ISp;
                        if (model != null)
                        {
                            var baseCard = CardConfig.Instance.GetCardByName(model.CardName);
                            if (_GameInfo.H1 && baseCard.GameVersion == 1
                                || _GameInfo.H2 && baseCard.GameVersion == 2
                                || _GameInfo.H3 && baseCard.GameVersion == 3)
                                spList.Add(model);
                        }
                    }
                }
            }
            // 读取之前的配置，加载到使用中，并且从列表删除
            var cList = LoadConfig();
            foreach (var c in cList)
            {
                var sp = spList.FirstOrDefault(t => t.Name == c);
                if (sp != null)
                {
                    ListViewItem item = new ListViewItem(sp.Name);
                    item.Tag = sp;
                    lsvUse.Items.Add(item);

                    spList.Remove(sp);
                }
            }
            // 剩下的列表加载到选择源
            foreach (var s in spList)
            {
                ListViewItem item = new ListViewItem(s.Name);
                item.Tag = s;

                lsvSource.Items.Add(item);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lsvUse.Items.Count >= 15)
            {
                MessageBox.Show("最多可选择15张稀有牌！");
                return;
            }

            if (lsvSource.SelectedItems != null && lsvSource.SelectedItems.Count > 0)
            {
                var item = lsvSource.SelectedItems[0];
                if (item.ForeColor == Color.Gray)
                    return;

                lsvSource.Items.Remove(item);
                lsvUse.Items.Add(item);
                lblUse.Text = $"选择稀有牌: {lsvUse.Items.Count}";

                SetDisabled(item.Tag as ISp, false);
            }
        }

        /// <summary>
        /// 同名卡稀有牌的处理，同名卡只能选一张
        /// </summary>
        /// <param name="sp"></param>
        /// <param name="disabled"></param>
        private void SetDisabled(ISp sp, bool disabled)
        {
            foreach (ListViewItem item in lsvSource.Items)
            {
                var s = item.Tag as ISp;
                if (s.CardName == sp.CardName)
                    item.ForeColor = disabled ? Color.Black : Color.Gray;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lsvUse.SelectedItems != null && lsvUse.SelectedItems.Count > 0)
            {
                var item = lsvUse.SelectedItems[0];

                lsvUse.Items.Remove(item);
                lsvSource.Items.Add(item);
                lblUse.Text = $"选择稀有牌: {lsvUse.Items.Count}";

                SetDisabled(item.Tag as ISp, true);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            List<string> lines = new List<string>();
            // 选择列表
            List<ISp> uList = new List<ISp>();
            foreach (ListViewItem item in lsvUse.Items)
            {
                lines.Add(item.Text);
                uList.Add(item.Tag as ISp);
            }
            SaveConfig(lines.ToArray());

            // 根据配置，从剩下的稀有牌从，随机选择出来给AI（剩下的？还是所有的？）
            List<ISp> aList = new List<ISp>();
            if (this._GameInfo.AI_XY == 1)
            {
                // 随机打乱排序然后取一样的张数
                spList.OrderBy(t => Guid.NewGuid().GetHashCode());
                aList = spList.Take(uList.Count).ToList();
            }
            else if(this._GameInfo.AI_XY==2)
            {
                aList = spList;
            }
            mainForm = new MainForm(this._GameInfo, uList, aList);
            this.DialogResult = DialogResult.OK;
        }

        private List<string> LoadConfig()
        {
            var configPath = "xySet.txt";

            if (File.Exists(configPath))
            {
                return File.ReadAllLines(configPath).ToList();
            }
            return new List<string>();
        }

        private void SaveConfig(string[] lines)
        {
            var configPath = "xySet.txt";

            File.WriteAllLines(configPath, lines);
        }

        private void lsv_SpChanged(object sender, EventArgs e)
        {
            var lsv = sender as ListView;
            if (lsv.SelectedItems.Count > 0)
            {
                var item = lsv.SelectedItems[0];
                var sp = item.Tag as ISp;
                lblName.Text = $"[{sp.Name}][{sp.SpScore + 2}分]";
                lblDesc.Text = sp.Desc;
            }
        }
    }
}
